package com.example.sy_2.servlet;

// CountDisplayServlet.java
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.io.IOException;

@WebServlet("/displayCount")
public class CountDisplayServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 从 ServletContext 中取出访问次数
        ServletContext context = getServletContext();
        int count = (int) context.getAttribute("visitCount");

        // 设置响应内容类型，并显示访问次数
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println("<h1>网站被访问了 " + count + " 次!</h1>");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 调用 doGet 方法处理 POST 请求
        doGet(request, response);
    }
}
